package com.steakliu.sun.client.netty.manager;

import com.steakliu.sun.client.netty.handler.SunClientHandler;
import com.steakliu.sun.common.meta.ServiceMeta;
import io.netty.channel.Channel;

import java.net.SocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 功能说明： Handler缓存
 * <p>
 * Original @Author: steakliu-刘牌, 2023-01-09  23:07
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
public class ClientHandlerCacheManager {

    private static final Map<String, SunClientHandler> CLIENT_HANDLER_CACHE = new ConcurrentHashMap<>();

    public static String generateHandlerKey(ServiceMeta serviceMeta){
       return String.join(":",serviceMeta.getHost(), String.valueOf(serviceMeta.getPort()));
    }

    public static void put(String handlerKey, SunClientHandler clientHandler){
        CLIENT_HANDLER_CACHE.put(handlerKey, clientHandler);
    }

    public static SunClientHandler get(String handlerKey){
       return CLIENT_HANDLER_CACHE.get(handlerKey);
    }

    public static void remove(Channel channel){
        String remoteAddress = channel.remoteAddress().toString();
        CLIENT_HANDLER_CACHE.remove(remoteAddress.replace("/",""));
    }

}
